package weaver.workflow.cms.xiao.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import weaver.wechat.request.HttpManager;

import java.io.IOException;
import java.util.Map;


/**
 * @author XiaoBokang
 * @create 2021/8/9 10:09
 */

public class RequestUtil {

    public static <T> T apiRequest(RequestBaseInfo requestInfo, Class<T> tClass){

        Map<String,String> headers = requestInfo.getHeaders();
        T result = null;

        switch(requestInfo.getType()){
            case "GET" :{
                String sendUrl = serializableUrl(requestInfo.getUrl(), (Map<String, Object>) requestInfo.getParams());
                HttpGet httpGet = new HttpGet(sendUrl);
                headers.forEach(httpGet::setHeader);
                result = apiSend(httpGet,tClass);
                }break;
            case "POST" :{
                HttpPost httpPost = new HttpPost(requestInfo.getUrl());
                headers.forEach(httpPost::setHeader);
                httpPost.setEntity(new StringEntity(JSON.toJSONString(requestInfo.getParams()),"UTF-8"));
                result = apiSend(httpPost,tClass);}break;
            case "PUT" :{
                HttpPut httpPut = new HttpPut(requestInfo.getUrl());
                headers.forEach(httpPut::setHeader);
                httpPut.setEntity(new StringEntity(JSON.toJSONString(requestInfo.getParams()),"UTF-8"));
                result = apiSend(httpPut,tClass);}break;
        }

        return result;
    }

    public static <T> T apiSend(
            HttpUriRequest httpRequest,
            Class<T> resClass
    ){
        DefaultHttpClient httpClient = HttpManager.getHttpClient();
        CloseableHttpResponse execute = null;
        T res = null;
        try {
            execute = httpClient.execute(httpRequest);
            if(execute.getStatusLine().getStatusCode() == 200){
                HttpEntity entity = execute.getEntity();
                String response= EntityUtils.toString(entity,"utf-8");
//                ObjectMapper mapper = new ObjectMapper();
//                CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class);
//                List<Map<String,Object>> list = mapper.readValue(response, listType);
//                result.put("res",list);
                ObjectMapper objectMapper = new ObjectMapper();
                res = objectMapper.readValue(response,resClass);
            }
        } catch (IOException e) {
            try {
                if(execute != null) {
                    execute.close();
                }
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            e.printStackTrace();
        }
        return res;
    }

    public static String serializableUrl(String url,Map<String,Object> params){
        if(params == null || params.isEmpty()){
            return url;
        }
        url += "?";
        for (Map.Entry entry: params.entrySet()){
            url += entry.getKey() + "=" + entry.getValue() + "&";
        }
        return url.substring(0,url.length()-1);
    }

}
